<?php

namespace app\api\controller\chat;

use app\common\controller\ApiBasic;
use app\common\service\im\GroupService;
use app\common\service\hrm\DeptService;
use app\common\service\sys\UserService;
use app\common\service\hrm\CompanyService;
use app\common\service\im\GroupUserService;

class Group extends ApiBasic
{

  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new GroupService();
  }

  public function getGroupList()
  {
    if (request()->isGet()) {
      $input = [
        'keywords' => input('keywords'),
        // 'uid' => input('uid'),
      ];
      $data = (new GroupService())->getGroupList($input, $this->user);
      return _result(1, $data, 3009);
    }
    return _result(0, [], 3010);
  }

  public function getDeptTree()
  {
    if (request()->isGet()) {
      $data = (new DeptService())->getDeptTreeUseAPI();
      return _result(1, $data, 3009);
    }
    return _result(0, [], 3010);
  }

  public function getEmployeeList()
  {
    if (request()->isGet()) {
      $data = (new UserService())->getEmployeeListUseAPI();
      return _result(1, $data, 3009);
    }
    return _result(0, [], 3010);
  }
  //创建群
  /**
   * 群-添加
   * @return array|string
   */
  public function groupadd()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $data['uid'] = $this->user['id'];
      return json($this->service->saveData($data));
    }
    if ($this->request->isGet()) {
      return _jok('ok', [
        'companyList' => (new CompanyService)->getCompanyTree(),
      ]);
    }
    return _jerr(1000);
  }
  /**
   * 群-编辑
   * @return array|string
   */
  public function groupedit()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      return json($this->service->saveData($data));
    }
    if ($this->request->isGet()) {
      $id = input('id/d', 0);
      $data = $this->service->getInfoById($id);
      $data['companyList'] = (new CompanyService)->getCompanyTree();
      if (isset($data['chat_head'])) {
        $data['chat_head_url'] = _img($data['chat_head']);
      }
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 群-删除
   */
  public function groupdel()
  {
    if ($this->request->isPost()) {
      $id = input('id/d', 0);
      if ($id === 0) {
        return _jerr(1002);
      }
      if ($this->service->delete($id)) {
        return _jok(1025);
      };
      return _jerr(1026);
    }
    return _jerr(1000);
  }

  //群用户列表
  public function groupuserlist()
  {
    if ($this->request->isGet()) {
      $gus = new GroupUserService();
      $data = input('param.');
      if (!isset($data['group_id']) || empty($data['group_id'])) {
        return json(_layui(0, []));
      }
      return json($gus->_list($data));
    }
    return _jerr(1000);
  }

  /**
   * 群用户添加
   * @return array|string
   */
  public function groupuseradd()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      return json((new GroupUserService())->addAllGroupUser($data));
    }
    if ($this->request->isGet()) {
      return _jok('ok', [
        'companyList' => CompanyService::getCompanyList(),
      ]);
    }
    return _jerr(1000);
  }





  /**
   * 群用户列表编辑
   * @return array|string
   */
  public function groupuseredit()
  {
    $gus = new GroupUserService();
    if ($this->request->isPost()) {
      $data = input('param.');
      return json($gus->saveData($data));
    }
    if ($this->request->isGet()) {
      $id = input('id/d', 0);
      $data = $gus->getInfoById($id);
      $data['companyList'] = CompanyService::getCompanyList();
      if (isset($data['chat_bg'])) {
        $data['chat_bg_url'] = _img($data['chat_bg']);
      }
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 群用户删除
   */
  public function groupuserdel()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      if ($data['group_id'] === 0) {
        return _jerr(1002);
      }
      if ((new GroupUserService())->delAllGroupUser($data)) {
        return _jok(1025);
      };
      return _jerr(1026);
    }
    return _jerr(1000);
  }
  /**
   * 群用户详情
   */
  public function groupuserdetails()
  {
    $gus = new GroupUserService();
    if ($this->request->isGet()) {
      $data = input('param.');
      $where[] = ['group_id', '=', $data['group_id']];
      $where[] = ['uid', '=', $data['uid']];
      $result = $gus->getListAll($where);
      // foreach($result as  $k=>$v){
      //   if(empty($v['nickname'])){
      //      $where1[] = ['group_id', '=', $v['group_id']];
      //      $where1[] = ['id', '=', $v['uid']];
      //      $user = (new UserService())->getInfoByWhere($where1);
      //      $result[$k]['nickname'] = $user['nickname'];
      //   }
      // }
      return _jok('ok', $result);
    }
  }
  //退出群
  public function quitgroup()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $data['uid'] = $this->user['id'];
      if ($data['group_id'] === 0) {
        return _jerr(1002);
      }
      if ($this->service->quitgroup($data)) {
        return _jok(1025);
      };
      return _jerr(1026);
    }

    return _jerr(1000);
  }
  //解散群
  public function dissolvegroup(){
    if ($this->request->isPost()) {
      $data = input('param.');
      $data['uid'] = $this->user['id'];
      if ($data['group_id'] === 0) {
        return _jerr(1002);
      }
      if ($this->service->dissolvegroup($data)) {
        return _jok(1025);
      };
      return _jerr(1026);
    }

    return _jerr(1000);
  }
}
